var api = require('../../config/api.js');
const app = getApp();
let that;
//缓存时间
let expire = 7200;
Page({
  data: {
    RootUrl: api.RootUrl,
    ImgRootUrl: app.globalData.ImgRootUrl,
    navList: [],
    categoryList: [],
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0
  },
  onLoad: function (options) {
    that = this;
    this.getCatalog();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    // 购物车角标
    if (app.globalData.cartGoodsCount != null) {
      app.cart.setCartBadge(app.globalData.cartGoodsCount);
    } else {
      app.cart.CartGoodsCount().then(function (res) {
        app.globalData.cartGoodsCount = res.data.cartTotal.goodsCount;
      }).catch((err) => {
        console.log(err)
      });
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.title + '-' + that.data.currentCategory.name,
      desc: that.data.currentCategory.descript,
      path: '/pages/catalog/catalog'
    }
  },
  getCatalog: function () {
    app.util.wxrequest(api.CatalogList).then(function (res) {
      that.setData({
        navList: res.data.categoryList,
        currentCategory: res.data.currentCategory
      });

      //2秒后,[异步]缓存所有分类,提高性能
      that.timer = setTimeout(function () {
        clearTimeout(that.timer);
        res.data.categoryList.forEach(function (v) {
          let cacheKey = 'Category' + v.id;
          //读取缓存
          let currentCategory = app.cache.get(cacheKey);
          if (currentCategory) {
            //console.log('cache存在', currentCategory);
          } else {
            app.util.wxrequest(api.CatalogCurrent, { id: v.id }).then(function (res) {
              //设置缓存
              app.cache.set(cacheKey, res.data.currentCategory, expire);
            });
          }
        });
      }, 2000);
    });
    
    app.util.wxrequest(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });
  },
  getCurrentCategory: function (id) {
    let cacheKey = 'Category' + id;
    //读取缓存
    let currentCategory = app.cache.get(cacheKey);
    if (currentCategory) {
      wx.setNavigationBarTitle({
        title: currentCategory.name + ' - 类'
      });
      that.setData({
        currentCategory: currentCategory
      });
    } else {
      app.util.wxrequest(api.CatalogCurrent, { id: id }).then(function (res) {
        wx.setNavigationBarTitle({
          title: res.data.currentCategory.name + ' - 分类'
        });
        that.setData({
          currentCategory: res.data.currentCategory
        });
        //设置缓存
        app.cache.set(cacheKey, res.data.currentCategory, expire);
      });
    }
  },
  switchCate: function (event) {
    let currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }
    this.getCurrentCategory(event.currentTarget.dataset.id);
  }
})